﻿package com.ufenlei.tool {
	import flash.display.Sprite;
	
	/**
	 * @author bbn
	 * Draw a RoundRectangle
	 */
	public class RoundRectangle extends Sprite {
		private var _w:int;
		private var _h:int;
		private var _tl:int;
		private var _tr:int;
		private var _br:int;
		private var _bl:int;
		private var _fa:Number;
		private var _fc:uint;
		private var _la:Number;
		private var _lc:uint;
		private var _lw:uint;
		public function RoundRectangle(
										vwidth:int,
										vheight:int,
										vFillAlpha:Number=0,
										vFillColor:uint=0xffffff,
										vLineAlpha:Number = 1,
										vLineColor:uint = 0x000000,
										vLineWeight:int=0,
										topLeft:int=0,
										topRight:int=0,
										bottomRight:int=0,
										bottomLeft:int=0){
			_w = Math.max(0,vwidth);
			_h = Math.max(0,vheight);
			_tl = Math.max(0,topLeft);
			_tr = Math.max(0,topRight);
			_br = Math.max(0,bottomRight);
			_bl = Math.max(0,bottomLeft);
			_fc =vFillColor;
			_lc = vLineColor;
			_lw = vLineWeight;
			_la = vLineAlpha;
			_fa = vFillAlpha;
			reDraw();
		}
		public function set w(vwidth:int):void{
			_w = Math.max(0,vwidth);
			reDraw();
		}
		public function set h(vheight:int):void{
			_h = Math.max(0,vheight);
			reDraw();
		}
		public function set fillColor(vFillColor:uint):void{
			_fc = vFillColor;
			reDraw();
		}
		public function set lineColor(vLineColor:uint):void{
			_lc = vLineColor;
			reDraw();
		}
		public function set lineWeight(vLineWeight:int):void{
			_lw = vLineWeight;
			reDraw();
		}
		public function set tl(topLeft:int):void{
			_tl = Math.max(0,topLeft);
			reDraw();
		}
		public function set tr(topRight:int):void{
			_tr = Math.max(0,topRight);
			reDraw();
		}
		public function set bl(bottomLeft:int):void{
			_bl = Math.max(0,bottomLeft);
			reDraw();
		}
		public function set br(bottomRight:int):void{
			_br = Math.max(0,bottomRight);
			reDraw();
		}
		public function set lineAlpha(la:Number):void{
			_la = la;
			reDraw();
		}
		public function set fillAlpha(fa:Number):void{
			_fa = fa;
			reDraw();
		}
		public function reDraw():void{
			this.graphics.clear();
			if(_lw >0 && _la >0){
				this.graphics.lineStyle(_lw,_lc,_la);
			}
			if(_fa > 0 ){
				this.graphics.beginFill(_fc,_fa);
			}			
			this.graphics.moveTo(0 , _tl);
			if(_tl>0){
				this.graphics.curveTo(0, 0, _tl, 0);
			}
			this.graphics.lineTo(_w - _tr, 0);
			if(_tr>0){
				this.graphics.curveTo(_w, 0, _w, _tr);
			}
			this.graphics.lineTo(_w, _h - _br);
			if(_br>0){
				this.graphics.curveTo(_w, _h, _w-_br, _h);
			}
			this.graphics.lineTo(_bl,_h);
			if(_bl>0){
				this.graphics.curveTo(0, _h, 0, _h-_bl);
			}
			this.graphics.lineTo(0, _tl);
			if(_fa > 0 ){
				this.graphics.endFill();	
			}						
		}
	}
}
